<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Manage extends CI_Controller {

        function __construct()
        {
                parent::__construct();
                $this->load->database();
                //$this->load->helper('url');
                $this->load->library('session');
                $this->load->library('table');
                $this->load->helper(array('form', 'url'));
        }
        function right_top()
        {
                $this->load->model('client_model');
                $result=$this->client_model->get_category();
                //print_r($result);
                if($result!=FALSE)
                {
                        $data['category']=$result;
                        $data['sign']=$this->session->userdata('admin_name');
                        $this->load->view('right_top',$data);
                }
                else {
                        echo 'No Category Records';
                }
        }
        function right_bottom()
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $limit=30;
                        $this->load->model('manage_books');
                        $this->load->library('pagination');
                        $config['base_url'] = site_url('manage/right_bottom');
                        $config['total_rows'] = $this->manage_books->get_books_num();
                        $config['per_page'] = $limit;
                        $this->pagination->initialize($config);
                        $offset=$this->uri->segment(3,0);
                        $data['page_result']=$this->manage_books->show_books($config['per_page'],$offset);
                        $data['page_link']=$this->pagination->create_links();
                        $data['sign']=$this->session->userdata('admin_name');
                        if($config['total_rows']==1)
                        {
                                $data['start']=0;
                        }
                        else
                        {
                                if($offset==$config['total_rows']-1)
                                {
                                        $data['start']=$offset-$limit;
                                }
                                else
                                {
                                        $data['start']=$offset;
                                }
                        }
                        $this->load->view('right_bottom',$data);
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function select_category_list($cate_id,$offset='0')
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                $limit=30;
                $this->load->model('manage_books');
                $data['cate_name']=$this->manage_books->category_name($cate_id);
                $this->load->library('pagination');
                $config['base_url'] = site_url('manage/select_category_list'.'/'.$cate_id).'/';
                $config['total_rows'] = $this->manage_books->every_category_nums($cate_id);
                $data['page_result']=$this->manage_books->every_category_list($limit,$offset,$cate_id);
                $config['uri_segment'] = 4;
                $config['per_page'] = $limit;
                $this->pagination->initialize($config);
                $data['cate_id']=$cate_id;
                if($config['total_rows']==1)
                {
                        $data['start']=0;
                }
                else
                {
                        if($offset==$config['total_rows']-1)
                        {
                                $data['start']=$offset-$limit;
                        }
                        else
                        {
                                $data['start']=$offset;
                        }
                }
                $data['page_link']=$this->pagination->create_links();
                $data['sign']=$this->session->userdata('admin_name');
                $this->load->view('show_list',$data);
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function edit_category($cate_id)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('admin_manage');
                        $data['msg']=$this->admin_manage->edit_category($cate_id);
                        $data['sign']=$this->session->userdata('admin_name');
                        $this->load->view('edit_category',$data);
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function update_category()
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('admin_manage');
                        $this->admin_manage->update_category();
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function delete_category($cate_id)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('admin_manage');
                        $this->admin_manage->delete_category($cate_id);
                }
                else
                {
                        $this->load->view('return');
                }
        }
   function upload_setp_one()
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('manage_books');
                        $b_id=$this->manage_books->upload_setp_one();
                        //echo $b_id;
                        if($b_id!=false)
                        {
                                redirect(site_url('manage/upload_pic'.'/'.$b_id), 'refresh ', 302);
                        }
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function upload_pic($b_id)
        {
                $this->load->model('manage_books');
                $sid=$this->manage_books->get_book_series_id($b_id);
                log_message("debug", "In upload picture sense, book:".$b_id." has series:".$sid);
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $sign=$this->session->userdata('admin_name');
                        $this->load->view('upload_pic', array('error' => ' ','b_id'=>$b_id,'sign'=>$sign, 'sid'=>$sid));
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function upload_setp_two($b_id)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                          $config['upload_path'] = 'SharePic/';
                          $config['allowed_types'] = 'gif|jpg|png';
                          $config['max_size'] = '5000';
                          $config['max_width']  = '1024';
                          $config['max_height']  = '768';
                          $config['file_name']  = date("YmdHis").rand(10000,99999);
                          $this->load->library('upload', $config);
                          if ( ! $this->upload->do_upload())
                          {
                           $error = array('error' => $this->upload->display_errors(),'b_id'=>$b_id,'sign'=>$this->session->userdata('admin_name'));
                           $this->load->view('upload_pic', $error);
                          }
                          else
                          {
                                $data =  $this->upload->data();
                                $url=$config['upload_path'].$data['file_name'];
                                $sql="update `books` set `pic`='$url' where `b_id`='$b_id'";
                                if($this->db->query($sql))
                                {
                                        $this->load->model("manage_books");
                                        $sid=$this->manage_books->get_book_series_id($b_id);
                                        if ($sid == 0) {
                                                redirect(site_url('welcome/upload_pdf/'.$b_id), 'refresh', 302);
                                        } else {
                                                redirect(site_url('manage/upload_plist'.'/'.$b_id), 'refresh ', 302);
                                        }
                                }
                          }
                }
                else
                {
                        $this->load->view('return');
                }
        }

        function upload_plist($b_id)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $data['b_id']=$b_id;
                        $data['sign']=$this->session->userdata('admin_name');
                        $this->load->view('upload_plist',$data);
                }
                else
                {
                        $this->load->view('return');
                }
        }

        function upload_book($b_id)
        {
                $this->load->model('manage_books');
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        set_time_limit(0);
                        $data['b_id']=$b_id;
                        $data['sign']=$this->session->userdata('admin_name');
                        $data['sid']=$this->manage_books->get_book_series_id($b_id);
                        log_message('debug', "book:".$b_id." series id:".$data['sid']);
                        $this->load->view('upload_book',$data);
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function edit_book($b_id)
        {
                $this->load->model('manage_books');
                $admin_name = $this->session->userdata('admin_name');
                $cate=$this->manage_books->get_category();
                if($admin_name!='')
                {
                        if($cate!=FALSE)
                        {
                                $data['cate']=$cate;
                                $data['b_id']=$b_id;
                                $data['msg']=$this->manage_books->get_book_message($b_id);
                                if($data['msg']['sid'] > 0) {
                                        $indexbook=$this->manage_books->get_book_message($data['msg']['sid']);
                                        $data['se_name']=$indexbook['bn'];
                                }
                                $slave=$this->manage_books->get_book_slave($b_id);
                                if(!empty($slave) && !($slave[0]['type_id'] == ''))
                                {
                                        $data['se_cate']=$this->manage_books->get_second_category($slave[0]['type_id']);
                                        $data['slave']=$slave;
                                }
                                //print_r($se_cate);
                                $data['sign']=$this->session->userdata('admin_name');
                                $this->load->view('edit_book',$data);
                        }
                        else
                        {
                                echo "You have not added books category!";
                        }
                }
                else
                {
                        $this->load->view('return');
                }
        }
        //删除书籍
        function del_book($cate_id,$start,$b_id)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->helper('file');
                        $this->load->model('manage_books');
                        if($this->manage_books->del_book($b_id))
                        {
                                if($cate_id=='index')
                                {
                                        redirect(site_url('manage/right_bottom'.'/'.$start), 'refresh ', 302);
                                }
                                else
                                {
                                        redirect(site_url('manage/select_category_list'.'/'.$cate_id.'/'.$start), 'refresh ', 302);
                                }
                        }
                        else
                        {
                                        echo "Delete Fail";
                        }

                }
                else
                {
                        $this->load->view('return');
                }
        }
        function del_sub_book($sid, $b_id) {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->helper('file');
                        $this->load->model('manage_books');
                        if($this->manage_books->del_book($b_id))
                        {
                                redirect(site_url('manage/get_book_message'.'/'.$sid), 'refresh ', 302);
                        }
                        else
                        {
                                        echo "Delete Fail";
                        }

                }
                else
                {
                        $this->load->view('return');
                }

        }

        function update_book_basic($b_id)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('manage_books');
                        $b_id=$this->manage_books->update_book_basic($b_id);
                        if($b_id!=false)
                        {
                                redirect(site_url('manage/upload_pic'.'/'.$b_id), 'refresh ', 302);
                        }
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function get_book_message($b_id)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('manage_books');
                        $msg=$this->manage_books->get_book_message($b_id);
                        $data['com_num']=$this->manage_books->get_comments_nums($msg['pid']);
                        $data['com']=$this->manage_books->get_ten_comments($msg['pid']);
                        $data['msg']=$msg;
                        $data['sign']=$this->session->userdata('admin_name');
                        $data['sub_books']=$this->manage_books->show_sub_books($b_id);
                        $this->load->view('display_book',$data);
                }
                else
                {
                        $this->load->view('return');
                }
        }

        function get_ten_comments($pid)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                $this->load->model('manage_books');
                $data['msg']=$this->manage_books->get_ten_comments($pid);
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function del_comment($pid,$b_id,$com_id)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('manage_books');
                        $result=$this->manage_books->del_comment($pid,$com_id);
                        if($result==true)
                        {
                                redirect(site_url('manage/get_book_message'.'/'.$b_id), 'refresh ', 302);
                        }
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function more_comments($pid,$offset='0')
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $limit=30;
                        $this->load->model('manage_books');
                        $comment=$this->manage_books->all_comments($limit,$offset,$pid);
                        $num=$this->manage_books->get_comments_nums($pid);
                        $this->load->library('pagination');
                        $config['base_url'] = site_url('manage/more_comments'.'/'.$pid).'/';
                        $config['total_rows'] = $num;
                        $data['page_result']=$comment;
                        $config['uri_segment'] = 4;
                        $config['per_page'] = $limit;
                        $this->pagination->initialize($config);
                        if($config['total_rows']==1)
                        {
                                $data['start']=0;
                        }
                        else
                        {
                                if($offset==$num-1)
                                {
                                        $data['start']=$offset-$limit;
                                }
                                else
                                {
                                        $data['start']=$offset;
                                }
                        }
                        $data['page_link']=$this->pagination->create_links();
                        $data['sign']=$this->session->userdata('admin_name');
                        $this->load->view('all_comments',$data);
                }
                else
                {
                        $this->load->view('return');
                }
        }
        function delete_com($pid,$offset,$com_id)
        {
                $admin_name = $this->session->userdata('admin_name');
                if($admin_name!='')
                {
                        $this->load->model('manage_books');
                        $result=$this->manage_books->del_comment($pid,$com_id);
                        redirect(site_url('manage/more_comments').'/'.$pid.'/'.$offset, 'refresh ', 302);
                }
                else
                {
                        $this->load->view('return');
                }
        }
}






























